#!/usr/bin/python

print "Content-Type: text/html\n\n";


import os, sys


# print sys.path

# sys.exit()

import MySQLdb
import htmlUtils
import dbUtils
import cgi;



# this gets around a nasty bug which
# assumes os.environ['USER'] will be set
import os
os.environ['USER'] = "apache";

import semantic;

DB_USERNAME = "root";
DB_HOST = "localhost";
DB_PASSWORD = "";
DB_DATABASE= "nlp";


db = MySQLdb.connect(host=DB_HOST, user=DB_USERNAME, passwd=DB_PASSWORD, db=DB_DATABASE);


#  - Eric Lieberman and Mike Lin
print """
<html>
<head>
  <title>SemanticSQL Dictionary</title>
  <link rel="StyleSheet" href="style.css">

  <script>
    function focusText() {
      var tbox = document.getElementById("input_textbox");
      tbox.focus();
    }
  </script>
</head>

<body onLoad="focusText()">
<font size=6><b>SemanticSQL Dictionary/Lexicon</b></font><br>
<font size=4><br>
<a href="index.py">Back to Parser</a><br>
<p>
<table border="0">
<tr>
<td>To add a verb choose a verb type and supply the morphological forms:<br>
<form action="dictionary.py" method="GET">
  <input type="hidden" name="action" value="input">
  <input type="radio" size=20 id="verbtype_radio" name="verbtype" value="1" selected="true" checked> Type-1 (Sleep, Fall, etc)<br>
  <input type="radio" size=20 id="verbtype_radio" name="verbtype" value="2"> Type-2 (Eat, Chase, etc)<br>
  <input type="radio" size=20 id="verbtype_radio" name="verbtype" value="3"> Type-3 (Give)<br>
  <input type="textbox" size=20 id="verbinf_textbox" name="verbinf"> Infinitive: fall<br>
  <input type="textbox" size=20 id="verbpast_textbox" name="verbpast"> Past: fell, kissed<br>
  <input type="textbox" size=20 id="verbpressing_textbox" name="verbpressing"> Present Singular: falls, kisses<br>
  <input type="textbox" size=20 id="verbperfect_textbox" name="verbperfect"> Perfect: fallen (optional)<br>
  <input type="submit" value="Submit">
</form>
<p></td>
<td>
To add a noun a noun type and word:<br>
<form action="dictionary.py" method="GET">
  <input type="hidden" name="action" value="addnoun">
  <input type="radio" size=20 id="verbtype_radio" name="nountype" value="Name" selected="true" checked> Name (John, Mary, etc)<br>
  <input type="radio" size=20 id="verbtype_radio" name="nountype" value="N"> Singular and Plural Noun (Book, Car, Tax, etc)<br>
  <input type="radio" size=20 id="verbtype_radio" name="nountype" value="N[mass +]"> Mass Noun (icecream, data, etc)<br>
  <input type="textbox" size=20 id="noun_textbox" name="noun"> The Noun (John, book, cat, icecream, etc)<br>
  <input type="textbox" size=20 id="noun_textbox" name="nounp"> The plural Noun (if not mass or name) (cats, books, etc)<br>
  <input type="submit" value="Submit">
</form>
<p></td>
</tr>
<tr>
<td>
Add an adverb:
<form action="dictionary.py" method="GET">
  <input type="hidden" name="action" value="addadverb">
  <input type="textbox" size=20 id="noun_textbox" name="adverb"> The Adverb (gracefully, etc)<br>
  <input type="submit" value="Submit">
</form>
</td>
<td>
Add an adjective:
<form action="dictionary.py" method="GET">
  <input type="hidden" name="action" value="addadjective">
  <input type="textbox" size=20 id="noun_textbox" name="adjective"> The Adjective (red, rabid, etc)<br>
  <input type="submit" value="Submit">
</form>
</td>
</tr>
</table>
"""

form = cgi.FieldStorage()
action = "none";
if (form.has_key("action")):
    action = form["action"].value;


if action == "none":
    "" # do nothing
elif action == "clear":
    dbUtils.clearTable(db, "Verb");

    htmlUtils.writeStatusMessage("Cleared the verb database");    
elif action == "input":
    verbtype = form["verbtype"].value
    verbinf = ""
    if form.has_key("verbinf"):
      verbinf  = "'"+form["verbinf"].value+"'"
    verbpast = ""
    if form.has_key("verbpast"):
      verbpast = "'"+form["verbpast"].value+"'"
    verbpressing = ""
    if form.has_key("verbpressing"):
      verbpressing = "'"+form["verbpressing"].value+"'"
    verbperfect = "NULL"
    if form.has_key("verbperfect"):
      verbperfect = "'"+form["verbperfect"].value+"'"
    
    if len(verbinf) > 2 and len(verbpast) > 2 and len(verbpressing) > 2:
      htmlUtils.writeStatusMessage("Input Verb: " + verbinf);
      sql = "INSERT INTO verb VALUES (%s)" % (",".join([verbtype,verbinf,verbpast,verbpressing,verbperfect]))
      print "<br>"+sql+"<br>"
      cursor = db.cursor()
      cursor.execute(sql)
elif action == "addnoun":
	nountype = "'"+form["nountype"].value+"'"
	# N[mass -, number plural] N[mass -, number singular]
	if form["nountype"].value != 'N':
		if form.has_key("noun"):
		  noun = "'"+form["noun"].value+"'"
		  htmlUtils.writeStatusMessage("Input Noun: " + noun)
		  sql = "INSERT INTO noun VALUES (%s)" % (",".join([nountype,noun,"NULL"]))
		  print "<br>"+sql+"<br>"
		  cursor = db.cursor()
		  cursor.execute(sql)
	else:
		if form.has_key("noun") and form.has_key("nounp"):
			noun = "'"+form["noun"].value+"'"
			nounp= "'"+form["nounp"].value+"'"
			htmlUtils.writeStatusMessage("Input Noun: " + noun +"/"+ nounp)
			sql = "INSERT INTO noun VALUES (%s)" % (",".join(["'N[mass -, number singular]'",noun,nounp]))
			print "<br>"+sql+"<br>"
			cursor = db.cursor()
			cursor.execute(sql)
			sql = "INSERT INTO noun VALUES (%s)" % (",".join(["'N[mass -, number plural]'",nounp,"NULL"]))
			print "<br>"+sql+"<br>"
			cursor.execute(sql)
elif action == "addadverb":
	if form.has_key("adverb"):
	  adverb = "'"+form["adverb"].value+"'"
	  htmlUtils.writeStatusMessage("Input Adverb: " + adverb)
	  sql = "INSERT INTO adverb VALUES (%s)" % (",".join([adverb]))
	  print "<br>"+sql+"<br>"
	  cursor = db.cursor()
	  cursor.execute(sql)
elif action == "addadjective":
	if form.has_key("adjective"):
	  adjective = "'"+form["adjective"].value+"'"
	  htmlUtils.writeStatusMessage("Input adjective: " + adjective)
	  sql = "INSERT INTO adjective VALUES (%s)" % (",".join([adjective]))
	  print "<br>"+sql+"<br>"
	  cursor = db.cursor()
	  cursor.execute(sql)

else:
    htmlUtils.writeStatusMessage("Unknown command: " + action);


print """
<p>
<font size=5><b>Verb Database Status:</b></font>

<p>
"""

print '<table><tr><td>'
print 'Verbs:<br>';
htmlUtils.writeTable(db, "verb");
print '</td>'

print '<td>'
print 'Nouns:<br>';
htmlUtils.writeTable(db, "noun");
print '</td></tr><tr><td>'

print 'Adverbs:<br>'
htmlUtils.writeTable(db, "adverb");
print '</td><td>'

print 'Adjectives:<br>'
htmlUtils.writeTable(db, "adjective");
print '</td></tr></table>'

print '</body>';

print '</html>';
print "\n";


